package com.yantu.controller.student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yantu.bean.ExamPaper;
import com.yantu.bean.ExamPaperAnswer;
import com.yantu.bean.TaskExam;
import com.yantu.mapper.ExamPaperAnswerMapper;
import com.yantu.mapper.ExamPaperMapper;
import com.yantu.mapper.TaskExamMapper;
import com.yantu.util.Result;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
@ApiModel("任务中心")
public class StudentTaskController {
    @Autowired
    ExamPaperMapper examPaperMapper;
    @Autowired
    TaskExamMapper taskExamMapper;
    @Autowired
    ExamPaperAnswerMapper examPaperAnswerMapper;

    //任务中心
    @GetMapping("/task")
    @ApiOperation("任务中心")
    public Result Task() {


        List<TaskExam> taskExams = taskExamMapper.selectList(null);
        List<Object> list = new ArrayList();

        for (TaskExam taskExam : taskExams) {
            Map<String, Object> map = new HashMap<>();

            map.put("id",taskExam.getId());
            map.put("title",taskExam.getTitle());
            //将task表和Exam_paper表对应起来
            QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
            if(taskExam.getId()!=null){
            queryWrapper.eq("task_exam_id",taskExam.getId());}
            ExamPaper examPaper = examPaperMapper.selectOne(queryWrapper);

            List<Object> list1 = new ArrayList();
            Map<String, Object> map1 = new HashMap<>();
            map1.put("examPaperId",examPaper.getId());
            map1.put("examPaperName",examPaper.getName());

            //将Exam_paper表和xam_paper_answer对应起来
            QueryWrapper<ExamPaperAnswer> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("exam_paper_id",examPaper.getId());
            ExamPaperAnswer examPaperAnswer = examPaperAnswerMapper.selectOne(queryWrapper1);
            if(examPaperAnswer!=null){
                map1.put("examPaperAnswerId",examPaperAnswer.getId());
                map1.put("status",examPaperAnswer.getStatus());
                list1.add(map1);
                map.put("paperItems",list1);
            }

            list.add(map);
        }
        System.out.println(list);
        return Result.succ(200, "查询成功",list);

    }


}
